/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.cef.command.listener;

import cef.command.Command;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.CommandPrintWriter;
import genomemap.cef.command.ClassifyGenes_Cmd;

import java.util.Map;
import java.util.Set;

/**
 * @author Susanta Tewari
 * @since Jun 12, 2011
 */
public class ClassifyGenes_PW extends CommandPrintWriter {

    /**
     * Method description
     *
     *
     * @param cmdOutputEvent
     *
     * @throws CommandOutputListenerException
     */
    @Override
    public void receivedEventImpl(final CommandOutputEvent cmdOutputEvent)
            throws CommandOutputListenerException {


        // 1. get the command
        final ClassifyGenes_Cmd command = (ClassifyGenes_Cmd) cmdOutputEvent.getSource();


        // 2. get the classified genes by the linkage groups
        final Map<Integer, Set<String>> map = command.getClassifiedGenes();


        // 3. process the map. Count the genes classified for each linkage group and the total.
        int totalGenesFound = 0;

        for (final Integer linkageGroup : map.keySet()) {

            final Set<String> genesFound = map.get(linkageGroup);

            getPrintWriter().println("\nAssignments for linkage group: " + linkageGroup + " Size: "
                                     + genesFound.size());

            totalGenesFound += genesFound.size();

            getPrintWriter().println("Genes: " + genesFound);
        }

        getPrintWriter().println("\nTotal # of genes found: " + totalGenesFound);


        // 4. flush
        getPrintWriter().flush();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public Class<? extends Command> getTargetCommandClass() {
        return ClassifyGenes_Cmd.class;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public CommandOutputListener newInstance() {
        return new ClassifyGenes_PW();
    }
}
